package de.blitzcoder.collide.gui;

import de.blitzcoder.collide.CollIDE;
import de.blitzcoder.collide.Config;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import de.blitzcoder.collide.icons.Icon;
import java.awt.Dimension;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

/**
 *
 * @author blitzcoder
 */
public class SplashScreen extends IDEFrame {

    private JLabel statusLabel;
    private JProgressBar progBar;
    private long timestamp;

    public SplashScreen() {
        super("CollIDE starting");

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setContentPane(contentPane);

        //getContentPane().setBackground(Color.BLACK);
        setContentPane(new TranscluentPanel());

        getContentPane().setLayout(new GridBagLayout());
        setUndecorated(true);
        setResizable(false);

        GridBagConstraints c = new GridBagConstraints();

        JLabel imgLabel = new JLabel();
        ImageIcon icon = Icon.load("splash.png");
        imgLabel.setIcon(icon);

        progBar = new JProgressBar(0, Config.getIntProperty("ui.splash.time", 10000)); // max number of status changes.
        progBar.setPreferredSize(new Dimension(10, 3));
        progBar.setForeground(Color.WHITE);
        progBar.setBackground(Color.BLACK);
        progBar.setBorderPainted(false);
        progBar.setValue(0);

        statusLabel = new JLabel("<html><b>Laden...</b></html>");
        statusLabel.setForeground(Color.WHITE);
        statusLabel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.WHITE));

        c.insets = new Insets(5, 5, 5, 5);

        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = c.BOTH;
        getContentPane().add(imgLabel, c);

        c.weighty = 0.0;
        c.gridy = 1;
        c.insets = new Insets(5, 5, 1, 5);
        getContentPane().add(statusLabel, c);
        c.gridy = 2;
        c.insets = new Insets(1, 1, 1, 1);
        getContentPane().add(progBar, c);

        pack();
        setVisible(true);

        timestamp = System.currentTimeMillis();

        TimerTask task = new TimerTask() {

            public void run() {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        progBar.setValue((int) (System.currentTimeMillis() - timestamp));
                    }
                });
            }
        };
        new Timer().schedule(task, 0, 100);


    }

    public void setStatus(String str) {
        statusLabel.setText("<html><b> [Version " + CollIDE.VERSION + "]  " + str + "...</b></html>");
    }

    public void dispose() {
        Config.setIntProperty("ui.splash.time", (int) (System.currentTimeMillis() - timestamp));
        super.dispose();
    }
}
